package com.vincent.tvui;

import android.content.Context;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目：TvLauncher 作者：907357 时间：2016/10/13 11:58 版本：1.0 描述：解析布局文件
 */

public final class CellLayoutInflater {
    private static final String TAG = "CellLayoutInflater";

    private static CellLayoutInflater INSTANCE = null;
    private Factory mFactory;

    private CellLayoutInflater() {
        mFactory = CellViewFactory.getInstance();
    }

    static CellLayoutInflater getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new CellLayoutInflater();
        }

        return INSTANCE;
    }

    void inflate(InputStream inputStream, CellLayout cellLayout) {
        Reader reader = null;

        try {
            reader = new InputStreamReader(inputStream, "UTF-8");
            inflate(reader, cellLayout);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void inflate(String json, CellLayout cellLayout) {
        StringReader stringReader = new StringReader(json);
        inflate(stringReader, cellLayout);
        stringReader.close();
    }

    void inflate(Reader reader, CellLayout cellLayout) {
        if (cellLayout == null || reader == null) {
            return;
        }

        JsonReader jsonReader = new JsonReader(reader);
        try {
            jsonReader.beginObject();

            String name;
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                switch (name) {
                    case "version":
                        Log.d(TAG, "version:" + jsonReader.nextInt());
                        break;
                    case "rowCell":
                        cellLayout.setRowCellCount(jsonReader.nextInt());
                        break;
                    case "columnCell":
                        cellLayout.setColumnCellCount(jsonReader.nextInt());
                        break;
                    case "cellMode":
                        cellLayout.setCellMode(jsonReader.nextInt());
                        break;
                    case "cellSize":
                        cellLayout.setCellSize(jsonReader.nextInt());
                        break;
                    case "widthGap":
                        cellLayout.setWidthGap(jsonReader.nextInt());
                        break;
                    case "heightGap":
                        cellLayout.setHeightGap(jsonReader.nextInt());
                        break;
                    case "debug":
                        cellLayout.setDebug(jsonReader.nextBoolean());
                        break;
                    case "cells":
                        createCells(jsonReader, cellLayout);
                        break;

                    default:
                        jsonReader.skipValue();
                }

            }

            jsonReader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                jsonReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void createCells(JsonReader jsonReader, CellLayout cellLayout) throws IOException {
        jsonReader.beginArray();


        CellLayout.CellLayoutParams cellLayoutParams = null;
        CellView cellView = null;
        int viewId = View.NO_ID;
        String type = "";
        Map<String, String> styles = null;
        String prefix = null;

        while (jsonReader.hasNext()) {
            jsonReader.beginObject();
            String name1;

            cellLayoutParams = cellLayout.generateDefaultLayoutParams();
            while (jsonReader.hasNext()) {
                name1 = jsonReader.nextName();
                switch (name1) {
                    case "id":
                        viewId = jsonReader.nextInt();
                        break;
                    case "type":
                        type = jsonReader.nextString();
                        break;
                    case "cellX":
                        cellLayoutParams.cellX = jsonReader.nextInt();
                        break;
                    case "cellY":
                        cellLayoutParams.cellY = jsonReader.nextInt();
                        break;
                    case "spanX":
                        cellLayoutParams.spanX = jsonReader.nextInt();
                        break;
                    case "spanY":
                        cellLayoutParams.spanY = jsonReader.nextInt();
                        break;
                    case "prefix":
                        prefix = jsonReader.nextString();
                        break;
                    case "styles":
                        styles = parseStyles(jsonReader);
                        break;
                    default:
                        jsonReader.skipValue();
                }

            }

            if (mFactory == null) {
                throw new RuntimeException("not set Factory");
            }

            cellView = mFactory.onCreateView(prefix, type, cellLayout.getContext(), styles);

            if (cellView != null && cellView instanceof View) {
                View t = (View) cellView;
                t.setId(viewId);
                t.setFocusable(true);
                cellLayout.addView(t, cellLayoutParams);
            }

            jsonReader.endObject();
        }
        jsonReader.endArray();
        cellLayout.requestLayout();
    }

    /**
     * 解析样式
     *
     * @param jsonReader
     * @return
     */
    private Map<String, String> parseStyles(JsonReader jsonReader) throws IOException {
        Map<String, String> styles = new HashMap<>();

        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            styles.put(jsonReader.nextName(), jsonReader.nextString());
        }
        jsonReader.endObject();

        return styles;
    }

    public void setCellViewController(CellViewController cellViewController) {
        if (mFactory != null) {
            mFactory.setCellViewController(cellViewController);
        }
    }

    interface Factory {
        CellView onCreateView(String prefix, String name, Context context, Map<String, String> styles);

        void setCellViewController(CellViewController controller);
    }

}
